import { User, UserProfile } from '@/types/data'
import { ProfileAction } from '@/types/store'

type ProfileState = {
  user: User
  userProfile: UserProfile
}

const initialState = {
  user: {},
  userProfile: {},
} as ProfileState

export const profile = (
  state = initialState,
  action: ProfileAction
): ProfileState => {
  // 我的页面 - 用户信息
  if (action.type === 'profile/get') {
    return {
      ...state,
      user: action.payload,
    }
  }
  // 个人信息-修改
  if (action.type === 'profile/getUserProfile') {
    return {
      ...state,
      userProfile: action.payload,
    }
  }

  if (action.type === 'profile/update') {
    return {
      ...state,
      userProfile: {
        ...state.userProfile,
        ...action.payload,
      },
    }
  }

  return state
}
